part of 'component.dart';

class CodeField extends StatefulWidget {
  const CodeField({
    Key? key,
    required this.phonecontroller,
    required this.controller,
    this.isNeedBorder = false,
    this.height,
  }) : super(key: key);
  final TextEditingController controller;
  final TextEditingController phonecontroller;
  final bool isNeedBorder;
  final double? height;
  @override
  _CodeFieldState createState() => _CodeFieldState();
}

class _CodeFieldState extends State<CodeField> {
  String actionText = '点击获取';
  int timeCount = 60;
  Timer? timer;
  bool isSending = false;
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    timer?.cancel();
  }

  void sendAction(String phoneNumber) async {
    if (isSending) {
      return;
    }
    var param = {
      "occurrenceTime": "",
      "oneParam": phoneNumber,
      "tenantId": "DSX-PEAR"
    };
    // ResultData response = await Api.sendSignCodeByAliyun(param);
    // if (response.isSuccess) {
    //   isSending = true;
    //   timer = Timer.periodic(const Duration(seconds: 1), (_) {
    //     if (timeCount == 0) {
    //       isSending = false;
    //       timer?.cancel();
    //       timeCount = 60;
    //       actionText = '点击获取';
    //     } else {
    //       timeCount--;
    //       actionText = '${timeCount}s';
    //     }
    //     setState(() {});
    //   });
    // }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height ?? 50.r,
      alignment: Alignment.center,
      decoration: widget.isNeedBorder
          ? BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5),
              border: Border.all(color: ColorsUtil.content, width: 0.5))
          : null,
      child: TextField(
        maxLength: 10,
        keyboardType: TextInputType.number,
        textAlignVertical: TextAlignVertical.center,
        controller: widget.controller,
        style: TextStyle(color: ColorsUtil.title, fontSize: 20.sp),
        decoration: InputDecoration(
          isCollapsed: true,
          counterText: '',
          contentPadding: widget.isNeedBorder
              ? EdgeInsets.symmetric(horizontal: 10.r)
              : EdgeInsets.zero,
          prefixIcon: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.asset('icon_code.png'.imgPathLogin,
                    width: 28.r, height: 28.r),
              ]),
          suffixIcon: InkWell(
            onTap: () => sendAction(widget.phonecontroller.text),
            child: Container(
              margin: EdgeInsets.all(5),
              padding: EdgeInsets.fromLTRB(8.r, 0, 8.r, 0),
              decoration: BoxDecoration(
                  color:
                      isSending ? const Color(0xff999999) : Color(0xff58c165),
                  borderRadius: BorderRadius.all(Radius.circular(25.r))),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    actionText,
                    style: TextStyle(color: ColorsUtil.white, fontSize: 14.sp),
                  ),
                ],
              ),
              height: 30.h,
              width: 75.w,
            ),
          ),
          hintText: '请输入验证码',
          hintStyle: TextStyle(color: const Color(0xffbbbbbb), fontSize: 20.sp),
          border: InputBorder.none,
          enabledBorder: widget.isNeedBorder ? getBorder() : InputBorder.none,
          disabledBorder: widget.isNeedBorder ? getBorder() : InputBorder.none,
          focusedBorder: widget.isNeedBorder ? getBorder() : InputBorder.none,
        ),
      ),
    );
  }

  InputBorder getBorder() {
    return const UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0xffeeeeee)));
  }
}
